package org.coder80.admin.user.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.coder80.admin.user.model.User;
import org.coder80.admin.user.service.UserService;
import org.framework.MapUtil;
import org.framework.enums.SortBy;
import org.framework.pager.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/user")
public class UserController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/add")
	public String add() {
		return "user/add";
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String, Object> save(HttpServletRequest request, HttpServletResponse response, @RequestBody User user, String dept) {
		System.out.println("xx:" + dept);
		try {
			if (user != null) {
				user.setCreateTime(new Date());
				int n = userService.create(user);
			}
			return MapUtil.toMap(200, "保存成功");
		} catch (Exception e) {
			return MapUtil.toMap(0, "保存失败");
		}
	}

	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, HttpServletResponse response, Integer currentPage,
			Integer pageRecorders, User user, ModelMap model) {

		PageBean<User> pageBean = new PageBean<User>();
		if (currentPage != null) {
			pageBean.setCurrentPage(currentPage);
		} else {
			pageBean.setCurrentPage(1);
		}
		if (pageRecorders != null) {
			pageBean.setPageRecorders(pageRecorders);
		} else {
			pageBean.setPageRecorders(10);
		}
		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("id", user.getId());
		if (user != null) {
			map.put("id", user.getId());
			map.put("name", user.getName());
		}
		userService.find4Page(pageBean, map, true, "id", SortBy.倒序);
//		userService.find4Page(pageBean, map, "id", SortBy.倒序);
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("id", user.getId());
		model.addAttribute("name", user.getName());
		return "user/list";
	}

	@RequestMapping(value = "/edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, Integer currentPage,
			Integer pageRecorders, Integer id, ModelMap model) {
		try {
			User user = userService.get(id);
			model.put("user", user);
		} catch (Exception e) {

		}
		return "user/edit";
	}

	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request, HttpServletResponse response,
			@RequestBody User user, String dept) {
		System.out.println("xx:" + dept);
		try {
			user.setModifyTime(new Date());
			userService.update(user);
			return MapUtil.toMap(200, "保存成功");
		} catch (Exception e) {
			return MapUtil.toMap(0, "保存失败");
		}
	}

	@RequestMapping(value = "/list2")
	public String list2() {
		return "user/list2";
	}

	@RequestMapping(value = "/del/batch", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchDel(HttpServletRequest request, HttpServletResponse response,
			String ids) {
		try {
			if (ids != null && ids.length() > 0) {
				for (String id : ids.split(",")) {
					userService.delete(Integer.parseInt(id));
				}
			}
			return MapUtil.toMap(200, "删除成功");
		} catch (Exception e) {
			return MapUtil.toMap(0, "删除失败！");
		}
	}

}
